<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="120px">
      <!-- <el-form-item label="父章节id，（只有2级）" prop="parentSectionId">
         <el-input v-model="dataForm.parentSectionId" placeholder="父章节id，（只有2级）"></el-input>
       </el-form-item>-->
      <el-form-item label="归属章节名称" >
        <el-input v-model="dataForm.courseName"  label="章节排序" disabled=""></el-input>
      </el-form-item>
      <el-form-item label="小节排序" prop="chapterNum">
        <el-input-number v-model="dataForm.chapterNum"  :min="1" label="章节排序"></el-input-number>
      </el-form-item>
      <el-form-item label="小节名称" prop="sectionName">
        <el-input v-model="dataForm.sectionName" placeholder="章节名称"></el-input>
      </el-form-item>
      <!--   <el-form-item label="排序" prop="orderNum">
           <el-input v-model="dataForm.orderNum" placeholder="排序"></el-input>
         </el-form-item>-->
      <el-form-item label="时长" prop="courseTime">
        <el-input v-model="dataForm.courseTime" placeholder="时长"></el-input>
      </el-form-item>
      <el-form-item label="是否上架" prop="status">
        <template>
          <el-radio-group v-model="dataForm.status">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="2">否</el-radio>
          </el-radio-group>
        </template>
      </el-form-item>
      <el-form-item label="小节描述" prop="remark">
        <el-input type="textarea" v-model="dataForm.remark" placeholder="章节描述"></el-input>
      </el-form-item>
      <el-form-item label="视频地址" prop="videoUrl">
        <el-input v-model="dataForm.videoUrl" placeholder="视频地址"></el-input>
      </el-form-item>
      <el-form-item label="视频网盘地址" prop="sectionPanUrl">
        <el-input v-model="dataForm.sectionPanUrl" placeholder="视频网盘地址"></el-input>
      </el-form-item>
      <el-form-item label="视频晚盘地址密码" prop="sectionPanPassword">
        <el-input v-model="dataForm.sectionPanPassword" placeholder="视频晚盘地址密码"></el-input>
      </el-form-item>
      <el-form-item label="创建人" prop="createUser">
        <el-input v-model="dataForm.createUser" disabled placeholder="创建人"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-input v-model="dataForm.createTime" disabled placeholder="创建时间"></el-input>
      </el-form-item>
      <el-form-item label="修改人" prop="updateUser">
        <el-input v-model="dataForm.updateUser" disabled placeholder="修改人"></el-input>
      </el-form-item>
      <el-form-item label="修改时间" prop="updateTime">
        <el-input v-model="dataForm.updateTime" disabled placeholder="修改时间"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      var validateInteger = (rule, value, callback) => {
        if (!this.dataForm.chapterNum && !/^[0-9]*[1-9][0-9]*$/.test(value)) {
          callback(new Error('请输入正整数'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        dataForm: {
          sectionId: 0,
          parentSectionId: '',
          courseId: '',
          sectionName: '',
          orderNum: '',
          courseTime: '',
          status: 1,
          videoUrl: '',
          sectionPanUrl: '',
          sectionPanPassword: '',
          createUser: '',
          createTime: '',
          updateUser: '',
          remark: '',
          updateTime: '',
          sectionList: '',
          chapterNum: '',
          teacherId: ''
        },
        dataRule: {
          parentSectionId: [
            {required: true, message: '父章节id，（只有2级）不能为空', trigger: 'blur'}
          ],
          courseId: [
            {required: true, message: '归属课程id不能为空', trigger: 'blur'}
          ],
          sectionName: [
            {required: true, message: '章节名称不能为空', trigger: 'blur'}
          ],
          courseTime: [
            {required: true, message: '时长不能为空', trigger: 'blur'}
          ],
          chapterNum: [
            {required: true, validator: validateInteger, trigger: 'blur'}
          ],
          status: [
            {required: true, message: '上架（1）、未上架（2）不能为空', trigger: 'blur'}
          ],
          videoUrl: [
            {required: true, message: '视频地址不能为空', trigger: 'blur'}
          ],
          sectionPanUrl: [
            {required: true, message: '视频网盘地址不能为空', trigger: 'blur'}
          ],
          sectionPanPassword: [
            {required: true, message: '视频晚盘地址密码不能为空', trigger: 'blur'}
          ]
          /* createUser: [
           { required: true, message: '创建人不能为空', trigger: 'blur' }
           ],
           createTime: [
           { required: true, message: '创建时间不能为空', trigger: 'blur' }
           ],
           updateUser: [
           { required: true, message: '修改人不能为空', trigger: 'blur' }
           ],
           updateTime: [
           { required: true, message: '修改时间不能为空', trigger: 'blur' }
           ] */
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.sectionId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.sectionId) {
            this.$http({
              url: this.$http.adornUrl(`/teach/section/info/${this.dataForm.sectionId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.parentSectionId = data.tCourseSection.parentSectionId
                this.dataForm.courseId = data.tCourseSection.courseId
                this.dataForm.sectionName = data.tCourseSection.sectionName
                this.dataForm.orderNum = data.tCourseSection.orderNum
                this.dataForm.courseTime = data.tCourseSection.courseTime
                this.dataForm.status = data.tCourseSection.status
                this.dataForm.videoUrl = data.tCourseSection.videoUrl
                this.dataForm.remark = data.tCourseSection.remark
                this.dataForm.sectionPanUrl = data.tCourseSection.sectionPanUrl
                this.dataForm.sectionPanPassword = data.tCourseSection.sectionPanPassword
                this.dataForm.createUser = data.tCourseSection.createUser
                this.dataForm.createTime = data.tCourseSection.createTime
                this.dataForm.updateUser = data.tCourseSection.updateUser
                this.dataForm.updateTime = data.tCourseSection.updateTime
                this.dataForm.chapterNum = data.tCourseSection.chapterNum
                this.dataForm.teacherId = data.tCourseSection.teacherId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        console.log()
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/teach/section/${!this.dataForm.sectionId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'sectionId': this.dataForm.sectionId,
                'parentSectionId': this.$route.params.sectionId,
                'courseId': this.$route.params.courseId,
                'sectionName': this.dataForm.sectionName,
                'orderNum': this.dataForm.orderNum,
                'courseTime': this.dataForm.courseTime,
                'status': this.dataForm.status,
                'videoUrl': this.dataForm.videoUrl,
                'remark': this.dataForm.remark,
                'sectionPanUrl': this.dataForm.sectionPanUrl,
                'sectionPanPassword': this.dataForm.sectionPanPassword,
                'createUser': this.dataForm.createUser,
                'createTime': this.dataForm.createTime,
                'updateUser': this.dataForm.updateUser,
                'updateTime': this.dataForm.updateTime,
                'chapterNum': this.dataForm.chapterNum,
                'teacherId': this.dataForm.teacherId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
